<?php
// $Id$

/**
 * @file
 * Implementation of bow class
 */
 
/**
 * Class to manage users bows
 */
class archeryBow {
	private $bows = NULL;
	private $current = NULL;
	
	/** Constructor
	 * Retrieves bow information from the database
	 * @param $uid User ID
	 */
	function archeryBow($uid) {
	  $result = db_query('SELECT * FROM {bow} WHERE `uid` = :uid', array(':uid' => $uid));
	  $i = 0;
	  foreach($result as $bow) {
		  $this->bows[$i] = $bow;
		  if($bow->main == 1) {
		    $this->current = $i;
		  }
		  $i++;
	  }
	  if($this->current == NULL) {
	    $this->current = 0;
	  }
  }
  
  /**
	 * Determine if the user has multiple bows
	 * @return TRUE if more than one bow
	 */
  function is_multiple() {
    return sizeof($this->bows) > 1;
  }
  
  /**
	 * Switch between user bows, if possible
	 * @return TRUE if switched
	 */
  function switch_bow() {
    if($this->is_multiple()) {
      $this->current = $this->current == 0 ? 1 : 0;
    }
  }
  
  /**
	 * Get the current bow ID
	 */
  function bid() {
    return $this->bows[$this->current]->bid;
  }
  
  /**
	 * Get the current bow type
	 * @param $full true for full name
	 */
  function type($full = false) {
    if($full) {
      return $this->bows[$this->current]->type == 'R' ? 'recurve' : 'compound';
    }
    return $this->bows[$this->current]->type;
  }
  
  /**
	 * Update a handicap field for the current bow.
	 * @param $hc New handicap value
	 * @param $col Column to update
	 */
  private function _set_handicap($hc,$col) {
    if($hc <= 101 && $hc >= 0) {
      db_update('bow')->fields(array($col -> $hc))->condition('bid', $this->bows[$this->current])->execute();
    } else throw new Exception('Invalid handicap supplied when trying to update.');
  }
  
  /*
	 * Update a classification field for the current bow.
	 * @param $class New classification value
	 * @param $col Column to update
	 */
  private function _set_class($class,$col) {
    //List allowed classification values
    $allowed = array('A','B','C','D','E','F','G','H','GMB','JMB','MB','JB','B','1st','2nd','3rd','Arc','UC');
    if(in_array($class,$allowed)) {
      db_update('bow')->fields(array($col -> $class))->condition('bid', $this->bows[$this->current])->execute();
    } else throw new Exception('Invalid classification supplied when trying to update.');
  }
  
  //Getters
  function OH() { return $this->bows[$this->current]->OH; }      //Get Outdoor Handicap
  function IH() { return $this->bows[$this->current]->IH; }      //Get Indoor Handicap
  function OC() { return $this->bows[$this->current]->OC; }      //Get Outdoor Class
  function IC() { return $this->bows[$this->current]->IC; }      //Get Indoor Class
  function SOH() { return $this->bows[$this->current]->SOH; }    //Get Starting Outdoor Handicap
  function SIH() { return $this->bows[$this->current]->SIH; }    //Get Starting Indoor Handicap
  function SOC() { return $this->bows[$this->current]->SOC; }    //Get Starting Outdoor Class
  function SIC() { return $this->bows[$this->current]->SIC; }    //Get Starting Indoor Class
  
  //Setters
  function set_OH($hc) { $this->_set_handicap($hc,'OH'); }      //Set Outdoor Handicap
  function set_IH($hc) { $this->_set_handicap($hc,'IH'); }      //Set Indoor Handicap
  function set_OC($class) { $this->_set_class($class,'OC'); }   //Set Outdoor Class
  function set_IC($class) { $this->_set_class($class,'IC'); }   //Set Indoor Class
  function set_SOH($hc) { $this->_set_handicap($hc,'SOH'); }    //Set Starting Outdoor Handicap
  function set_SIH($hc) { $this->_set_handicap($hc,'SIH'); }    //Set Starting Indoor Handicap
  function set_SOC($class) { $this->_set_class($class,'SOC'); } //Set Starting Outdoor Class
  function set_SIC($class) { $this->_set_class($class,'SIC'); } //Set Starting Indoor Class
}
